//
//  UIImageExtensions.m
//  MantuPlay
//
//  Created by Markus Sintonen on 9.4.2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "UIImageExtensions.h"
#import "ImageIO.h"

@implementation UIImage (UIImageExtensions)

+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize {	
	UIGraphicsBeginImageContext( newSize );
	[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
	UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();

	return newImage;
	/*CGImageRef cgimage = image.CGImage;
	CGColorSpaceRef colorspace = CGImageGetColorSpace(cgimage);
	CGContextRef context = CGBitmapContextCreate(NULL, newSize.width, newSize.height,
												CGImageGetBitsPerComponent(cgimage),
												CGImageGetBytesPerRow(cgimage),
												colorspace,
												CGImageGetAlphaInfo(cgimage));
	CGColorSpaceRelease(colorspace);

	if(context == NULL)
	return nil;

	// draw image to context (resizing it)
	CGContextDrawImage(context, CGRectMake(0, 0, newSize.width, newSize.height), cgimage);
	// extract resulting image from context
	CGImageRef imgRef = CGBitmapContextCreateImage(context);
	CGContextRelease(context);
	
	return [UIImage imageWithCGImage:imgRef];*/
}
- (UIImage*)scaleImageToSize:(CGSize)newSize keepAspectRatio:(BOOL)keepAspect {
	CGSize size = newSize;
	if(keepAspect) {
		CGSize originalSize = self.size;
		float aspect = originalSize.height / originalSize.width;
		float newHeight = size.width * aspect;
		
		size.height = newHeight;
	}

	return [UIImage imageWithImage:self scaledToSize:size];
}

- (UIImage*)scaleImageToSize:(CGSize)newSize {
	return [self scaleImageToSize:newSize keepAspectRatio:NO];
}

-(BOOL)writeJPEGImageToFile:(NSString*)path compressionQuality:(float)quality{
	/*CGImageDestinationRef imageDestination = 
		CGImageDestinationCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path], kUTTypePNG, 1, NULL);
	
	if(imageDestination == NULL) {
		NSLog(@"Could not create imagedestination.");
		return NO;
	}
	
	CGImageDestinationAddImage(imageDestination, self.CGImage, NULL);
	
	CGImageDestinationFinalize(imageDestination);
	
	CFRelease(imageDestination);*/
	
	NSData *imageData = UIImageJPEGRepresentation(self, quality);
	if(!imageData) return NO;
	
	NSFileManager *fileManager = [NSFileManager defaultManager];
	return ([fileManager createFileAtPath:path contents:imageData attributes:nil]);
}

/*-(NSData*)customDataRepresentation {
	CGImageRef cgimage = self.CGImage;
	size_t w = CGImageGetWidth(cgimage);
	size_t h = CGImageGetHeight(cgimage);
	size_t bitsPerComponent = CGImageGetBitsPerComponent(cgimage);
	size_t bitsPerPixel = CGImageGetBitsPerPixel(cgimage);
	size_t bytesPerRow = CGImageGetBytesPerRow(cgimage);
	uint32_t bitmapInfo = CGImageGetBitmapInfo(cgimage);
	CFDataRef cfImageData = CGDataProviderCopyData(CGImageGetDataProvider(cgimage));
	
	
	NSMutableData *data = [[NSMutableData alloc] initWithCapacity:CFDataGetLength(cfImageData) + 5*sizeof(size_t) + sizeof(uint32_t)];
	[data appendBytes:&w length:sizeof(size_t)];
	[data appendBytes:&h length:sizeof(size_t)];
	[data appendBytes:&bitsPerComponent length:sizeof(size_t)];
	[data appendBytes:&bitsPerPixel length:sizeof(size_t)];
	[data appendBytes:&bytesPerRow length:sizeof(size_t)];
	[data appendBytes:&bitmapInfo length:sizeof(uint32_t)];

	CFRelease(cfImageData);
	
	return (NSData*)[data autorelease];
}

+(UIImage*)imageFromCustomDataRepresentation:(NSData*)data {
	const char *bytes = (const char*)[data bytes];
	unsigned int pos = 0;
	
	size_t w, h, bitsPerComponent, bitsPerPixel, bytesPerRow;
	uint32_t bitmapInfo;
	memcpy(&w, bytes+pos, sizeof(size_t));
	pos += sizeof(size_t);
	memcpy(&h, bytes+pos, sizeof(size_t));
	pos += sizeof(size_t);
	memcpy(&bitsPerComponent, bytes+pos, sizeof(size_t));
	pos += sizeof(size_t);
	memcpy(&bitsPerPixel, bytes+pos, sizeof(size_t));
	pos += sizeof(size_t);
	memcpy(&bytesPerRow, bytes+pos, sizeof(size_t));
	pos += sizeof(size_t);
	memcpy(&bitmapInfo, bytes+pos, sizeof(uint32_t));
	pos += sizeof(uint32_t);
	CGDataProviderRef prov = CGDataProviderCreateWithData(NULL, bytes+pos, [data length]-pos, NULL);
	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
	
	CGImageRef cgimage = CGImageCreate(w, h, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpace,
										bitmapInfo, prov, nil, NO, kCGRenderingIntentDefault);
	
	CGColorSpaceRelease(colorSpace);
	CFRelease(prov);
	UIImage *image = [UIImage imageWithCGImage:cgimage];
	CFRelease(cgimage);
	return image;
}*/

@end
